package com.example.demo.math;

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.UUID;

/**
 * 数字相关测试
 *
 * @author ldl
 */
public class MathContorller {

    public static void main(String[] args) {
        /*System.out.println(Math.max(-2, 1));
        System.out.println(Math.max(-1, 1));
        System.out.println(Math.max(0, 1));
        System.out.println(Math.max(1, 1));
        System.out.println(Math.max(2, 1));*/

        System.out.println("13067961500".substring(1));

        URL url;
        try {

            url = new URL("https://img2.fr-trading.com/0/5_15_2323218_800_492.jpg.webp");
            String filename = UUID.randomUUID().toString().replace("-", "") + System.currentTimeMillis() + ".png";

            InputStream fis = url.openStream();
            byte[] byteArr = toByteArray(fis);
            fis.close();
            String dir = "/data/51pla/";
            // 输出文件路径
            String filedPath = dir + filename;
            try {
                File file = new File(filedPath);
                FileOutputStream fos = new FileOutputStream(file);
                FileChannel channel = fos.getChannel();
                channel.write(ByteBuffer.wrap(byteArr));
                channel.close();
                //封装json
                JSONObject outData = new JSONObject();
                outData.put("path", filedPath);
                outData.put("size", file.length());
            } catch (Exception e) {
            }
        } catch (Exception e) {

        }
    }

    private static byte[] toByteArray(InputStream in) throws IOException {

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024 * 4];
        int n = 0;
        while ((n = in.read(buffer)) != -1) {
            out.write(buffer, 0, n);
        }
        return out.toByteArray();
    }
}
